package org.homerepair

import org.apache.commons.lang.builder.HashCodeBuilder

class ContractorUserContractorRole implements Serializable {

	ContractorUser contractorUser
	ContractorRole contractorRole

	boolean equals(other) {
		if (!(other instanceof ContractorUserContractorRole)) {
			return false
		}

		other.contractorUser?.id == contractorUser?.id &&
			other.contractorRole?.id == contractorRole?.id
	}

	int hashCode() {
		def builder = new HashCodeBuilder()
		if (contractorUser) builder.append(contractorUser.id)
		if (contractorRole) builder.append(contractorRole.id)
		builder.toHashCode()
	}

	static ContractorUserContractorRole get(long contractorUserId, long contractorRoleId) {
		find 'from ContractorUserContractorRole where contractorUser.id=:contractorUserId and contractorRole.id=:contractorRoleId',
			[contractorUserId: contractorUserId, contractorRoleId: contractorRoleId]
	}

	static ContractorUserContractorRole create(ContractorUser contractorUser, ContractorRole contractorRole, boolean flush = false) {
		new ContractorUserContractorRole(contractorUser: contractorUser, contractorRole: contractorRole).save(flush: flush, insert: true)
	}

	static boolean remove(ContractorUser contractorUser, ContractorRole contractorRole, boolean flush = false) {
		ContractorUserContractorRole instance = ContractorUserContractorRole.findByContractorUserAndContractorRole(contractorUser, contractorRole)
		if (!instance) {
			return false
		}

		instance.delete(flush: flush)
		true
	}

	static void removeAll(ContractorUser contractorUser) {
		executeUpdate 'DELETE FROM ContractorUserContractorRole WHERE contractorUser=:contractorUser', [contractorUser: contractorUser]
	}

	static void removeAll(ContractorRole contractorRole) {
		executeUpdate 'DELETE FROM ContractorUserContractorRole WHERE contractorRole=:contractorRole', [contractorRole: contractorRole]
	}

	static mapping = {
		id composite: ['contractorRole', 'contractorUser']
		version false
	}
}
